package com.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.CourseDao;
import com.opensymphony.xwork2.ActionContext;
import com.po.Course_skillPO;
import com.po.SkillPO;
@Controller
public class SkillAction {
	@Autowired
	private CourseDao cd;
	
	/**
	 * 删除指定的技能
	 * @return
	 */
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		int vid = Integer.parseInt(request.getParameter("vid"));
		request.setAttribute("vid", vid);
		//删除给定ID的技能
		cd.deleteSkillInfo(id);
		return "delete";
	}
	
	/**
	 * 空跳界面，添加新的技能信息界面
	 */
	public String preAddSkill(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String cid = request.getParameter("cid");
		
		request.setAttribute("cid", cid);
		return "add_skill";
	}
	
	
	
	
	/**
	 * 给课程等级中添加新的技能
	 * @return
	 */
	public String addSkill() {
		// 保存技能在数据库中的技能表中，并拿到插入技能的ID值
		HttpServletRequest request = ServletActionContext.getRequest();

		SkillPO skillPO = new SkillPO();
		skillPO.setSkill_name(request.getParameter("name"));
		skillPO.setSkill_content(request.getParameter("intro"));
		cd.InsertSkillInfo(skillPO);

		int skillId = skillPO.getSkill_id();
		// 拿到的课程的ID值
		int courseId = Integer.parseInt(request.getParameter("id"));

		// 将课程ID和技能ID关联起来保存在第三张关联表中
		Course_skillPO conn = new Course_skillPO();
		conn.setCourse_id(courseId);
		conn.setSkill_id(skillId);

		cd.insertConnection(conn);

		return null;
	}
	/**
	 * 修改技能数据时，回显旧数据
	 */
	public String updateEcho(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		int vid = Integer.parseInt(request.getParameter("vid"));
		request.setAttribute("id", id);
		request.setAttribute("cid", vid);
		SkillPO skillPO = new SkillPO();
		
		skillPO = cd.findSkillEchoById(id);
		ActionContext.getContext().put("skillPO", skillPO);
		return "updateEcho";
	}
	
	
	
	/**
	 * 修改技能的信息
	 * @return
	 */
	public String update() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id"));
		SkillPO skillPO = new SkillPO();
		skillPO = new SkillPO();
		skillPO.setSkill_name(request.getParameter("name"));
		skillPO.setSkill_content(request.getParameter("intro"));
		skillPO.setSkill_id(id);
		
		cd.updateSkillInfo(skillPO);
		return null;
	}
}
